import { http } from '@renderer/plugins/axios'
import useUtil from './useUtil'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

export default () => {
  const shareDirList = ref<ApiPage<ShareDirModel>>()
  const shareFileList = ref<ApiPage<ShareFileModel>>()
  const queryParams = reactive({
    page: 1,
    limit: 21,
  })
  const dirFileParams = reactive({
    dir_id: 0,
    page: 1,
    limit: 21,
  })
  const getDocDir = useUtil().request(async (params = { limit: 21, page: 1 }) => {
    try {
      queryParams.limit = params.limit ?? 21
      shareDirList.value = await http.request({
        url: '/getDocDir',
        method: 'GET',
        params: Object.assign({}, queryParams, params),
      })
    } catch (error) {
      console.log(error)
    }
  })
  const createDocDir = useUtil().request(async (params: { dir_name: string }) => {
    try {
      await http.request({
        url: '/createDocDir',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '创建成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const delDir = useUtil().request(async (params: { dir_id: number }) => {
    try {
      await http.request({
        url: '/delDir',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '删除成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const getDirFile = useUtil().request(async (params: { dir_id: number; limit: number; page: number }) => {
    try {
      const queryParams = Object.assign({}, dirFileParams, params)
      shareFileList.value = await http.request({
        url: '/getDirFile',
        method: 'GET',
        params: queryParams,
      })
    } catch (error) {
      console.log(error)
    }
  })
  const updateFileName = useUtil().request(async (params: { file_id: number; file_name: string }) => {
    try {
      await http.request({
        url: '/updateFileName',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '修改成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const delDirFile = useUtil().request(async (params: { file_id: number }) => {
    try {
      await http.request({
        url: '/delDirFile',
        method: 'POST',
        data: params,
      })
      ElMessage({ type: 'success', message: '删除成功' })
    } catch (error) {
      console.log(error)
    }
  })
  const uploadDocFile = useUtil().request(
    async (params: { dir_id: string; file_name: string; file_url: string; file_size: string }) => {
      try {
        await http.request({
          url: '/uploadDocFile',
          method: 'POST',
          data: params,
        })
        ElMessage({ type: 'success', message: '上传成功' })
      } catch (error) {
        console.log(error)
      }
    },
  )
  return {
    getDocDir,
    shareDirList,
    queryParams,
    createDocDir,
    delDir,
    getDirFile,
    shareFileList,
    dirFileParams,
    updateFileName,
    delDirFile,
    uploadDocFile,
  }
}
